package com.yjkf.zh.core.utils;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.PrintWriter;
import java.util.zip.GZIPOutputStream;


/**
 * HttpServletRequest的封裝
 */
public class ServletRequestUtil {
        private HttpServletRequest request = null;
        private HttpServletResponse response = null;

        public ServletRequestUtil(HttpServletRequest request,HttpServletResponse response){
            this.request = request;
            this.response = response;
        }
        /**
         * 判断参数是否为空
         * @param
         * @return
         * @throws Exception
         */
        public boolean isParamStrValid(String s)throws Exception{
            boolean result = true;
            if((s==null)||("null".equalsIgnoreCase(s))||("".equalsIgnoreCase(s))){
                result = false;
            }
            return result;
        }
        /**
         * 当参数为空时给出默认值
         * （处理参数时，一种是用默认值，如果传入的参数必填的话，则要在程序中抛出异常，而不是给出默认值。）
         * @param
         * @return
         * @exception
         * @author liugx
         */
        public String setDefaultIfMissing(String s,String replacement) throws Exception{
            String result = s;
            try{
                if(!this.isParamStrValid(result)){
                    result = replacement;
                }
            }catch(Exception ex){
                ex.printStackTrace();
            }
            return result;
        }
        /**
         * 对参数进行解码操作
         * @param str
         * @return
         * @throws Exception
         * @author liugx
         */
        public String decodeUrlParam(String s) throws Exception{
            String result = s;
            try{
                result = java.net.URLDecoder.decode(result, "UTF-8");
            }catch (Exception ex){
                System.out.println(ex.getMessage());
                ex.printStackTrace();
            }
            return result;
        }
        /**
         * 过虑表单的特殊字符
         * @param
         * @return
         * @exception
         * @author liugx
         */
        public String filterSpecialChars(String s){
            if(!hasSpecialChars(s)) return s;
            StringBuffer sb = new StringBuffer(s.length());
            char c;
            for(int i=0;i<s.length();i++){
                c = s.charAt(i);
                switch(c){
                    case '<':sb.append("&lt;");break;
                    case '>':sb.append("&qt;");break;
                    case '"':sb.append("&quot;");break;
                    case '&':sb.append("&amp;");break;
                    default:sb.append(c);
                }
            }
            return sb.toString();
        }
        /**
         * 得到压缩输出
         * @return
         * @throws Exception
         * @author liugx
         */
        public PrintWriter getGzipPrintWriter()throws Exception{
            PrintWriter out;
            if(isGzipSupoorted()){
                out = new PrintWriter(new GZIPOutputStream(this.response.getOutputStream()));
            }else{
                out = response.getWriter();
            }
            return out;
        }
        /**
         * 关闭压缩输出
         * @return
         * @throws Exception
         * @author liugx
         */
        public void closeGzipPrintWriter(PrintWriter out) throws Exception{
            out.close();
        }

        /**
         * 表单是否含有特殊字符
         * @param
         * @return
         * @exception
         * @author liugx
         */
        private static boolean hasSpecialChars(String input){
            boolean flag = false;
            if((input!=null)&&(input.length()>0)){
                char c;
                for(int i=0;i<input.length();i++){
                    c = input.charAt(i);
                    switch(c){
                        case '<':flag = true;break;
                        case '>':flag = true;break;
                        case '"':flag = true;break;
                        case '&':flag = true;break;
                    }
                }
            }
            return flag;
        }
        /**
         * 是否支持压缩格式的传送
         * @param
         * @return
         * @exception
         * @author liugx
         */
        private boolean isGzipSupoorted(){
            String encoding = this.request.getHeader("Accept-Encoding");
            return ((encoding != null)&&(encoding.indexOf("gzip")!=-1));
        }
        public HttpServletRequest getRequest() {
            return request;
        }
        public void setRequest(HttpServletRequest request) {
            this.request = request;
        }
        public HttpServletResponse getResponse() {
            return response;
        }
        public void setResponse(HttpServletResponse response) {
            this.response = response;
        }

}
